package com.start.admin.vo;

import com.alibaba.fastjson2.JSONObject;
import com.start.admin.entity.StartSysUser;
import com.start.admin.entity.StartSysUserManageDept;
import com.start.admin.entity.StartSysUserRole;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class StartSysUserVo extends StartSysUser {

    @Schema(name = "角色")
    private String roleIds;

    @Schema(name = "管理部门")
    private String adminDeptIds;

    public static StartSysUserVo getInstance(StartSysUser user) {
        return JSONObject.parseObject(JSONObject.toJSONString(user), StartSysUserVo.class);
    }

    public static StartSysUserVo getInstance(StartSysUser user, List<StartSysUserRole> roles, List<StartSysUserManageDept> userDepts) {
        String roleIds = null;
        if (roles != null && roles.size() > 0) {
            roleIds = String.join(",", roles.stream().map(StartSysUserRole::getRoleId).collect(Collectors.toList()));
        }
        String deptIds = null;
        if (userDepts != null && userDepts.size() > 0) {
            deptIds = String.join(",", userDepts.stream().map(StartSysUserManageDept::getDeptId).collect(Collectors.toList()));
        }
        return getInstance(user, roleIds, deptIds);
    }

    public static StartSysUserVo getInstance(StartSysUser user, String roleIds, String adminDeptIds) {
        StartSysUserVo startSysUserVo = getInstance(user);
        startSysUserVo.setRoleIds(roleIds);
        startSysUserVo.setAdminDeptIds(adminDeptIds);
        return startSysUserVo;
    }


}
